package com.example.qqsave;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ManageActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnAdd, btnDel, btnSearch, btnUpdate;
    EditText mNameEdit, mPhoneEdit;
    TextView mShowText;
    MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);

        myHelper = new MyHelper(this);
        initView();
    }

    private void initView() {
        mShowText = findViewById(R.id.showTime);
        mNameEdit = findViewById(R.id.editText3);
        mPhoneEdit = findViewById(R.id.editText4);
        btnAdd = findViewById(R.id.btnAdd);
        btnDel = findViewById(R.id.btnDel);
        btnSearch = findViewById(R.id.search);
        btnUpdate = findViewById(R.id.update);
        btnAdd.setOnClickListener(this);
        btnDel.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (view.getId()) {
            case R.id.btnAdd:
                name = mNameEdit.getText().toString();
                phone = mPhoneEdit.getText().toString();
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name", name);
                values.put("phone", phone);
                db.insert("information", null, values);
                db.close();
                Toast.makeText(this, "信息已添加", Toast.LENGTH_LONG).show();
                break;
            case R.id.search:
                Toast.makeText(this, "searching ^_^", Toast.LENGTH_LONG).show();
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null, null, null, null, null, null);
                if (cursor.getCount() == 0) {
                    mShowText.setText("");
                    Toast.makeText(this, "null", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    mShowText.setText("Name: " + cursor.getString(1) + " :Tel: " + cursor.getString(2));
                }
                while (cursor.moveToNext()){
                    mShowText.append("\n"+"Name: " + cursor.getString(1) + " :Tel: " + cursor.getString(2));
                }
                cursor.close();
                db.close();
                break;
            case R.id.btnDel:
                db = myHelper.getWritableDatabase();
                db.delete("information", null, null);
                db.close();
                Toast.makeText(this, "done", Toast.LENGTH_LONG).show();
                mShowText.setText("");
                break;
            case R.id.update:
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("phone", phone = mPhoneEdit.getText().toString());
                db.update("information", values, "name=?", new String[]{mNameEdit.getText().toString()});
                Toast.makeText(this, "done", Toast.LENGTH_LONG).show();
                db.close();
                break;
        }
    }

    class MyHelper extends SQLiteOpenHelper {
        public MyHelper(@Nullable Context context) {
            super(context, "my.db", null, 1);
        }


        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sql = "CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(40),phone VARCHAR(40))";
            sqLiteDatabase.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}
